import Vue from 'vue'
import VueRouter from 'vue-router'
import ConsoleIndex from '../views/console/index.vue'
import LoginView from '@/views/login/index.vue'

import ProductView from '@/views/console/product/ProductView.vue'
import ProductTypeView from '@/views/console/product/ProductTypeView.vue'

import HomeView from '@/views/console/HomeView.vue'
import SalesView from '@/views/console/product/SalesView.vue'
import BrandView from '@/views/console/brand/BrandView.vue'
import OrderView from '@/views/console/order/OrderView.vue'
import OrderProductView from '@/views/console/order/OrderProductView.vue'
import ShopCartView from '@/views/console/shopCart/ShopCartView.vue'
import ShopCartProductView from '@/views/console/shopCart/ShopCartProductView.vue'
import UserManageView from '@/views/console/system/UserManageView.vue'
import RotManageView from '@/views/console/system/RotManageView.vue'
import SearchHistoryView from '@/views/console/system/SearchHistoryView.vue'
import ReceivingAddressView from '@/views/console/system/ReceivingAddressView.vue'

Vue.use(VueRouter)

const routes = [
  {
    path: '/console',
    name: 'console',
    component: ConsoleIndex,
    redirect: '/console/1-1',
    children: [
      {
        path: '/console/1-1',
        name: '仪表盘',
        component: HomeView
      },
      {
        path: '/console/2-1',
        name: '商品表',
        component: ProductView
      },
      {
        path: '/console/2-2',
        name: '商品类型表',
        component: ProductTypeView
      },
      {
        path: '/console/2-3',
        name: '商品销售表',
        component: SalesView
      },
      {
        path: '/console/3-1',
        name: '品牌表',
        component: BrandView
      },
      {
        path: '/console/4-1',
        name: '订单表',
        component: OrderView
      },
      {
        path: '/console/4-2',
        name: '订单商品表',
        component: OrderProductView
      },
      {
        path: '/console/5-1',
        name: '购物车表',
        component: ShopCartView
      },
      {
        path: '/console/5-2',
        name: '购物车商品表',
        component: ShopCartProductView
      },
      {
        path: '/console/6-1',
        name: '用户管理',
        component: UserManageView
      },
      {
        path: '/console/6-2',
        name: '轮播图管理',
        component: RotManageView
      },
      {
        path: '/console/6-3',
        name: '搜索历史管理',
        component: SearchHistoryView
      },
      {
        path: '/console/6-4',
        name: '收货地址管理',
        component: ReceivingAddressView
      }
    ]
  },
  {
    path: '/',
    name: 'login',
    meta: {
      noRequireAuth: true
    },
    component: LoginView
  }
]

const router = new VueRouter({
  routes
})

router.beforeEach((to,from,next) => {
  const user = localStorage.getItem("user");
   //路由不需要认证
   if (to.meta.noRequireAuth) {
    next()
  } else {
    // 判断是否有登录缓存
    if (user) {
      next()
    } else {
      next({
        path: '/'
      })
    }
  }
})

export default router
